package structures.xmltable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import util.GlobalUtils;

import domain.DadServicesDAO;

public class ServiceParameterTable implements Serializable{
	
	private static final long serialVersionUID = 6171958602198142345L;

	private ServiceParameterTableHeader headerInfo;

	private int maximumIndex;

	private DadServicesDAO service;
	private List<ServiceBaseTypeParameter> serviceBaseTypeParameters;

	private boolean kysMissingParameterPresent;
	private boolean updating;
	
	public ServiceParameterTable(DadServicesDAO service
			,String fcid, String tcid, String uid, String ctid
			) {
		super();
		init(service);	
		this.headerInfo = new ServiceParameterTableHeader(fcid, tcid, uid, ctid);
	}

	public ServiceParameterTable(DadServicesDAO service, ServiceParameterTableHeader headerInfo){
		super();
		init(service);	
		this.headerInfo = headerInfo;
	}
	public ServiceParameterTable() {
		super();
		maximumIndex = 0;
		this.service = new DadServicesDAO();
		this.serviceBaseTypeParameters = new ArrayList<ServiceBaseTypeParameter>();
		headerInfo = new ServiceParameterTableHeader();
	}
	
	
	private void init(DadServicesDAO service){
		maximumIndex = 0;
		this.service = service;
		this.serviceBaseTypeParameters = new ArrayList<ServiceBaseTypeParameter>();
	}
	public void add(Integer index, ServiceBaseTypeParameter esiServiceParameter){
		maximumIndex = (serviceBaseTypeParameters.size() > maximumIndex ? serviceBaseTypeParameters.size() : maximumIndex);
		esiServiceParameter.setBagIndex(maximumIndex++);
		esiServiceParameter.setUpdating(updating);
		if(index == null)
			serviceBaseTypeParameters.add(esiServiceParameter);
		else
			serviceBaseTypeParameters.add(index, esiServiceParameter);
	}
	public void add(ServiceBaseTypeParameter esiServiceParameter){
		add(null, esiServiceParameter);
	}
	
	public void removeSimpleType(int index){
		for (int i = 0 ; i < serviceBaseTypeParameters.size() ; ++i) {
			if(serviceBaseTypeParameters.get(i).getBagIndex() == index){
				serviceBaseTypeParameters.remove(i);
				break;
			}
		}
	}
	
	public ServiceBaseTypeParameter getSimpleType(int index){
		for (int i = 0 ; i < serviceBaseTypeParameters.size() ; ++i) {
			if(serviceBaseTypeParameters.get(i).getBagIndex() == index){
				return serviceBaseTypeParameters.get(i);
			}
		}
		return null;
	}
	
	public boolean kysAnalyzed(){
		boolean analyzed = false;
		if(serviceBaseTypeParameters != null && serviceBaseTypeParameters.size()>0){
			analyzed = serviceBaseTypeParameters.get(0).isKysAnalyzed();
		}
		return analyzed;
	}

	public void clear(){
		serviceBaseTypeParameters.clear();
		maximumIndex = 0;
		kysMissingParameterPresent = false;
	}

	public String getServiceName(){
		if(service == null)
			return "Service Not Selected";
		return service.getServicename();
	}
	
	public String getDescription(){
		if(service == null)
			return "";
		return service.getDescription();
	}

	public boolean isLongServiceName(){
		boolean isLong = false;
		if(service != null){
			isLong = service.getServicename().length() > 50;
		}
		return isLong;
	}
	
	public String getVisualServiceName(int size){
		if(service != null)
			return GlobalUtils.getSizeLimitedText(service.getServicename(), size);
		return null;
	}
	public void updatingParameter(ServiceBaseTypeParameter parameter){
		updating = true;
		parameter.setUpdating(true);
	}
	public void clearUpdatingParameters(){
		updating = false;
		for (ServiceBaseTypeParameter esiServiceParameter : serviceBaseTypeParameters) {
			esiServiceParameter.setUpdating(false);
		}
	}
	public DadServicesDAO getService() {
		return service;
	}

	public void setService(DadServicesDAO service) {
		this.service = service;
	}

	public void setServicename(String serviceName) {
		if(service == null)
			service = new DadServicesDAO();
		this.service.setServicename(serviceName);
	}
	public void setDescription(String description) {
		if(service == null)
			service = new DadServicesDAO();
		this.service.setDescription(description);
	}

	public String getFcid() {
		return headerInfo.getFcid();
	}

	public void setFcid(String fcid) {
		headerInfo.setFcid(fcid);
	}

	public String getTcid() {
		return headerInfo.getTcid();
	}

	public void setTcid(String tcid) {
		headerInfo.setTcid(tcid);
	}

	public String getUid() {
		return headerInfo.getUid();
	}

	public void setUid(String uid) {
		headerInfo.setUid(uid);
	}

	public String getCtid() {
		return headerInfo.getCtid();
	}
	public ServiceParameterTable clone(){
		ServiceParameterTable clone = new ServiceParameterTable(service,headerInfo.clone());
		for (ServiceBaseTypeParameter esiServiceParameter : serviceBaseTypeParameters) {
			clone.add(esiServiceParameter.clone());
		}
		return clone;
	}
	
	public ServiceParameterTableHeader getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(ServiceParameterTableHeader headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<ServiceBaseTypeParameter> getServiceBaseTypeParameters() {
		return serviceBaseTypeParameters;
	}

	public void setServiceBaseTypeParameters(
			List<ServiceBaseTypeParameter> serviceBaseTypeParameters) {
		this.serviceBaseTypeParameters = serviceBaseTypeParameters;
	}

	public boolean isKysMissingParameterPresent() {
		return kysMissingParameterPresent;
	}

	public void setKysMissingParameterPresent(boolean kysMissingParameterPresent) {
		this.kysMissingParameterPresent = kysMissingParameterPresent;
	}

	public boolean isUpdating() {
		return updating;
	}

	public void setUpdating(boolean updating) {
		this.updating = updating;
	}
	
	public String getServiceNameHdrStr(){
		String sonuc = getServiceName().equals("asd") ? "header" : "";
		return sonuc;
	}
}
